//
//  FoodProjectDetailViewController.m
//  B段项目
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "FoodProjectDetailViewController.h"
#import <UIImageView+WebCache.h>
#import "MBProgressHUD.h"
#import "XMHNetWorkongMethods.h"
#import "FitHeader.h"
#import "FoodProjectDetailCell.h"
#import "NSXFirstPageWEBViewController.h"
#import "CoreDataManager.h"
#import "NSXFirstPageListModel.h"
#import "FoodDetailModel.h"

@interface FoodProjectDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *sectionArray;
@property(nonatomic,strong)NSMutableArray *sectionjianjieArray;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation FoodProjectDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"列表";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    _sectionjianjieArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = [self createHeaderView];
    _tableView.tableFooterView = [[UITableView alloc]init];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    [self getData];
}
#pragma mark cell样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    FoodProjectDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[FoodProjectDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] ID];
    
    [cell.thumbImageView sd_setImageWithURL:[NSURL URLWithString:[[_dataArray[indexPath.section] objectAtIndex:indexPath.row] thumb]]];
    cell.titleLabel.text = [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] title];
    cell.categoryLabel.text = [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] category];
    cell.ageLabel.text = [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] age];
    cell.effectLabel.text =  [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] effect1];;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rotationAngleDegrees = 15;
    CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI / 180);
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, rotationAngleRadians, 0.0, 0.0, 1.0);
    transform = CATransform3DTranslate(transform, -500 * FitWidth, 0.0, 0.0);
    UIView *card = [cell contentView];
    card.layer.transform = transform;
    card.layer.opacity = 0.2;
    
    [UIView animateWithDuration:0.3f animations:^{
        card.layer.transform = CATransform3DIdentity;
        card.layer.opacity = 1;
    }];
}

#pragma mark cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    NSXFirstPageWEBViewController *nsxVC = [[NSXFirstPageWEBViewController alloc]init];
    nsxVC.NSXModel = [_dataArray[indexPath.section] objectAtIndex:indexPath.row];
    nsxVC.idString = [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] ID];
    nsxVC.modelString = [[_dataArray[indexPath.section] objectAtIndex:indexPath.row] title];
    [self.navigationController pushViewController:nsxVC animated:YES];
}

#pragma mark section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

#pragma mark section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30 * FitHeight + [self measureHeightWithString:_sectionjianjieArray[section]];
}
#pragma mark section头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 25 * FitHeight + [self measureHeightWithString:_sectionjianjieArray[section]])];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, -5 * FitHeight, self.view.frame.size.width, 20 * FitHeight)];
    titleLabel.textColor = [UIColor colorWithRed:0.33 green:0.62 blue:0.6 alpha:1];
    titleLabel.text = _sectionArray[section];
    [view addSubview:titleLabel];
    
    UILabel *jianjieLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, titleLabel.frame.origin.y + titleLabel.frame.size.height + 5 * FitHeight, self.view.frame.size.width - 20 * FitWidth, 5 * FitHeight + [self measureHeightWithString:_sectionjianjieArray[section]])];
    jianjieLabel.text = _sectionjianjieArray[section];
    jianjieLabel.numberOfLines = 0;
    jianjieLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
    [view addSubview:jianjieLabel];
    
    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}

#pragma mark cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95 *FitHeight;
}
#pragma mark cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}
#pragma mark 创建头视图
-(UIView *)createHeaderView
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 190 * FitHeight + [self measureHeightWithString:_jianjie])];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, 180 * FitHeight)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_thumbString]];
    [headerView addSubview:imageView];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 155 * FitHeight, headerView.frame.size.width, 25 * FitHeight)];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.25f;
    [imageView addSubview:backView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, 155 * FitHeight, headerView.frame.size.width, 25 * FitHeight)];
    titleLabel.text = _titleString;
    titleLabel.font = [UIFont systemFontOfSize:17 * FitWidth];
    titleLabel.textColor = [UIColor whiteColor];
    [imageView addSubview:titleLabel];
    
    UILabel *jianjieLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * FitWidth, imageView.frame.size.height + 5 * FitHeight, self.view.frame.size.width - 10 * FitWidth, [self measureHeightWithString:_jianjie])];
    jianjieLabel.text = _jianjie;
    jianjieLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
    jianjieLabel.textColor = [UIColor colorWithRed:0.37 green:0.37 blue:0.37 alpha:1];
    jianjieLabel.numberOfLines = 0;
    [headerView addSubview:jianjieLabel];
    
    headerView.backgroundColor = [UIColor whiteColor];

    return headerView;
}
#pragma mark 自适应高度
-(CGFloat)measureHeightWithString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth], NSFontAttributeName, nil];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

-(void)getData
{
    [XMHNetWorkongMethods getDataVyString:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.topic.detail.php?id=%@", _ID] BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"tlist"];
        for (NSDictionary *dic in array) {
            NSMutableArray *a = [NSMutableArray array];
           [_sectionArray addObject:[dic objectForKey:@"title"]];
            [_sectionjianjieArray addObject:[dic objectForKey:@"jianjie"]];
            NSArray *arr = [dic objectForKey:@"list"];
            for (NSDictionary *d in arr) {
                NSXFirstPageListModel *model = [[NSXFirstPageListModel alloc]init];
                [model setValuesForKeysWithDictionary:d];
                [a addObject:model];
            }
            [_dataArray addObject:a];
        }
        [_tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
